import machine
import zk
import esp32
import lcd12864
import PFC8563
import utime
from machine import ADC, Pin,I2C, deepsleep
from machine import RTC

SHT31_address       =   68
SHT20_address       =   64
T_measurement       =   bytes([0b11110011]) 
RH_measurement      =   bytes([0b11110101])
Write_user_register =   bytes([0b11100110])
Read_user_register  =   bytes([0b11100111])
Soft_reset          =   bytes([0b11111110])

adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)    # set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
adc.width(ADC.WIDTH_12BIT)  

wake1 = Pin(27, mode = Pin.IN)
p2 = machine.Pin(2, machine.Pin.OUT)
rtc = machine.RTC()
str=bytes()
s=[zk.n32,zk.ASCII,zk.n16,zk.xj]
i2c = I2C(1, scl=Pin(18), sda=Pin(19), freq=400000)

for n in s:
    for i in n:
        str=str+i
    print(len(str))
rtc.memory(str)
print("in1")
def p32(str,x=0,y=6):
    rm=rtc.memory()
    for n in range(2):
        lcd12864.setaddress(x,n+y)
        for i in str:
            if ord(i)>=48 and ord(i)<=57:
                b=(ord(i)-48)*16+1499+n*8
                lcd12864.write(rm[b:b+7])
            if ord(i)== 45 :
                b=(10)*16+1499+n*8
                lcd12864.write(rm[b:b+7])  #-
            if ord(i) == 46:
                b=11*16+1499+n*8
                lcd12864.write(rm[b:b+7])  #.
            if ord(i) == 67:
                b=14*16+1499+n*8
                lcd12864.write(rm[b:b+7])  #C
            if ord(i) == 86:
                b=12*16+1499+n*8
                lcd12864.write(rm[b:b+7])  #V
            if ord(i) == 37:
                b=13*16+1499+n*8
                lcd12864.write(rm[b:b+7]) #%
            if ord(i) == 32:
                b=15*16+1499+n*8
                lcd12864.write(rm[b:b+7]) #%
def st1f(t=0,r=0,u=0):
    lcd12864.print8("%.2fV  "%(u),80,4)
    p32("%.2fC  "%(t),0,6)
    p32("%.2f%%"%(r),80,6)
    # p32("%.2f"%(u),96,4)
def gettime():
        da=i2c.readfrom_mem(81,2,8)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        m=((da[1]>>4) * 10) + (da[1]&0x0f)
        h=((da[2]>>4) * 10) + (da[2]&0x0f)
        d=((da[3]>>4) * 10) + (da[3]&0x0f)
        w=((da[4]>>4) * 10) + (da[4]&0x0f)
        mo=((da[5]>>4) * 10) + (da[5]&0x0f)
        y=((da[6]>>4) * 10) + (da[6]&0x0f)+2000
        return (s,m,h,d,w,mo,y)

def callback1(str):
    print(str+"        ")
    lcd12864.print8(str,0,5)
def gett():
    i2c.writeto(68,b"\x2c\x06")
    utime.sleep_ms(30)
    raw=i2c.readfrom(68,6)
    (t,h)=(raw[0] << 8) + raw[1], (raw[3] << 8) + raw[4]
    temp = -45 + (175 * (t / 65535))
    v=adc.read()
    v=v*7.06/4096
    return (temp,100 * (h / 65535),v)

def showSlow(ds=0):
    t=gettime()
    lcd12864.st(t[2],t[1],t[6],t[5],t[3],t[4])

    st1f(*gett()) 
    if ds==1 :
        da=i2c.readfrom_mem(104,0,1)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        machine.deepsleep((60-s)*1000)

def showFast():
    leds=machine.Pin(14,Pin.IN,Pin.PULL_DOWN)
    showSlow()
    k=0
    for i in range(60):
        if(wake1.value()==1):
            k=k+1
            lcd12864.print8("%02d"%(k),0,5)
        else:
            k=0
            lcd12864.print8("               ",0,5)
        if k==3:
            PFC8563.sync(callback1)
        da=i2c.readfrom_mem(81,2,1)
        s=((da[0]>>4) * 10) + (da[0]&0x0f)
        strs=("%02d"%(s))
        if(s==0): 
            showSlow()
        lcd12864.print8(strs,81,2)
        t=rtc.datetime()
        print(i)
        st1f(*gett()) 
        utime.sleep_us(1000000-t[7])


        
    lcd12864.print8("    ",81,2)
    leds=machine.Pin(14,machine.Pin.IN,Pin.PULL_UP)
    machine.deepsleep((60-s)*1000)
    # machine.deepsleep(5000)



if machine.wake_reason() == 2:
    # da=i2c.readfrom_mem(81,2,1)
    # t=rtc.datetime()
    # showSlow()
    # p2.value(1)
    # utime.sleep_ms(10)
    # p2.value(0)
    # machine.deepsleep((5)*1000)
    pass
if machine.wake_reason() == 0:
    lcd12864.lcdInit()
    lcd12864.clearScreen()
    showFast()
if machine.wake_reason() == 4:  
    da=i2c.readfrom_mem(81,2,1)
    s=((da[0]>>4) * 10) + (da[0]&0x0f)
    t=rtc.datetime()
    showSlow()
    machine.deepsleep((60-s)*1000)
